package com.qf.hsr.advice;

import org.aspectj.lang.ProceedingJoinPoint;

import java.util.Arrays;

/**
 * @author q
 * @version V1.0
 * @date 2021/11/09
 **/
public class TxAdvice {

    public void begin(){
        System.out.println("--- 开启事务 --->");
    }

    public void commit(Object obj){
        System.out.println("=== 提交事务 --->");
    }

    public void rollback(Exception e){
        System.out.println("### 回滚事务 --->" + e.getMessage());
    }

    public void release(){
        System.out.println("*** 释放资源 --->");
    }

    public Object around(ProceedingJoinPoint pjp){
        Object result = null;
        try {
            //前置通知
            String methodName = pjp.getSignature().getName();
            Object[] args = pjp.getArgs();
            System.out.println("---开启事务-------->" + methodName + "  参数是:" + Arrays.asList(args));
            result = pjp.proceed();   //调用目标方法
            //返回通知
            System.out.println("===提交事务-------->" + result);
        } catch (Throwable e) {
            //e.printStackTrace();
            //异常通知
            System.out.println("....回滚事务-------->" + e.getMessage());
        } finally {
            //后置通知
            System.out.println("****释放资源-------->");
        }

        return result;
    }
}
